package com.dxx.D201910312055.auto;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Toast;

import com.dxx.D201910312055.dxx.AutoAbstract;
import com.dxx.D201910312055.dxx.utils.FindAccessibilityNodeInfosByClassName;

import static com.dxx.D201910312055.dxx.AutoService.mService;
import static java.lang.System.out;

public class Auto_Lightning_box extends AutoAbstract {
    //    属于自己的脚本是否开启
    private static boolean isAction = false;
    private static boolean isBreak = false;
    //    服务器数据
    public static String[] ClassNameData = {"", "", ""};

    @Override
    public void onStart() {
        isBreak = false;
        isAction = true;
        if (checkPackInfo("c.l.a")) {
            PackageManager packageManager = mService.getPackageManager();
            Intent intent = packageManager.getLaunchIntentForPackage("c.l.a");
            mService.startActivity(intent);
        } else {
            isAction = false;
            Toast.makeText(mService, "脚本失败,指定app不存在", Toast.LENGTH_SHORT).show();
        }


    }

    @Override
    public void onRun(AccessibilityEvent event) {


        if (isAction) {
//            单走
            new Thread(new Runnable() {
                public void run() {
                    onRunOne();
                }
            }).start();


//            try {
//
//                List<AccessibilityNodeInfo> a = mService.getRootInActiveWindow().findAccessibilityNodeInfosByViewId("c.l.a:id/tab_image");
//
//                if (!a.isEmpty()) {
//                    a.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                }
//            } catch (Exception e) {
//
//            }
//            try {
//
//                List<AccessibilityNodeInfo> a = mService.getRootInActiveWindow().findAccessibilityNodeInfosByViewId("c.l.a:id/tab_image");
//
//                if (!a.isEmpty()) {
//                    a.get(0).performAction(AccessibilityNodeInfo.ACTION_CLICK);
//                }
//            } catch (Exception e) {
//
//            }
//            Utils.mPerformAction(mService.getRootInActiveWindow(), "android.support.v7.widget.RecyclerView", AccessibilityNodeInfo.ACTION_SCROLL_BACKWARD);
//
//            Utils.behindPerformAction(mService.getRootInActiveWindow(), "android.widget.TextView", AccessibilityNodeInfo.ACTION_CLICK,1);
//            Utils.behindPerformAction(mService.getRootInActiveWindow(), "android.widget.ImageView", AccessibilityNodeInfo.ACTION_CLICK);

//            AccessibilityNodeInfo a = Utils.behindPerformAction(mService.getRootInActiveWindow(), "android.widget.RelativeLayout", AccessibilityNodeInfo.ACTION_CLICK, 4);

//            new FindAccessibilityNodeInfosByClassName().frontFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), "android.widget.TextView",5 ).performAction(AccessibilityNodeInfo.ACTION_CLICK);

//            new FindAccessibilityNodeInfosByClassName().behindFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), ClassNameData[0], 2).performAction(AccessibilityNodeInfo.ACTION_CLICK);
//            new FindAccessibilityNodeInfosByClassName().behindFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), ClassNameData[1], 4).performAction(AccessibilityNodeInfo.ACTION_CLICK);

//            new FindAccessibilityNodeInfosByClassName().behindFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), "android.widget.RelativeLayout", 4).performAction(AccessibilityNodeInfo.ACTION_CLICK);
//            new FindAccessibilityNodeInfosByClassName().behindFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), "android.widget.RelativeLayout", 2).performAction(AccessibilityNodeInfo.ACTION_CLICK);


        }
    }

    public void onRunOne() {

        if (isAction) {
            //控件单走
            isAction = false;

            long currentTimeMillisLong = System.currentTimeMillis() + 65000;
            while (System.currentTimeMillis() < currentTimeMillisLong) {
                if (isBreak) {
                    return;
                }
            }

            new FindAccessibilityNodeInfosByClassName().behindFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), ClassNameData[0], 2).performAction(AccessibilityNodeInfo.ACTION_CLICK);
//            new FindAccessibilityNodeInfosByClassName().behindFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), "android.widget.RelativeLayout", 2).performAction(AccessibilityNodeInfo.ACTION_CLICK);


            currentTimeMillisLong = System.currentTimeMillis() + 25000;
            while (System.currentTimeMillis() < currentTimeMillisLong) {
                if (isBreak) {
                    return;
                }
            }

            new FindAccessibilityNodeInfosByClassName().behindFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), ClassNameData[1], 4).performAction(AccessibilityNodeInfo.ACTION_CLICK);
//            new FindAccessibilityNodeInfosByClassName().behindFindAccessibilityNodeInfosByClassName(mService.getRootInActiveWindow(), "android.widget.RelativeLayout", 4).performAction(AccessibilityNodeInfo.ACTION_CLICK);

//            自控件递归
            isAction = true;
            onRunOne();

        }
    }

    @Override
    public void onStop() {
        isAction = false;
        isBreak = true;
    }
}
